<?php

/**
 * 
 * ClientEngage: ClientEngage Visitor Voice (http://www.clientengage.com)
 * Copyright 2012, ClientEngage (http://www.clientengage.com)
 *
 * You must have purchased a valid license from CodeCanyon in order to have 
 * the permission to use this file.
 * 
 * You may only use this file according to the respective licensing terms 
 * you agreed to when purchasing this item on CodeCanyon.
 * 
 * 
 * 
 *
 * @author          ClientEngage <contact@clientengage.com>
 * @copyright       Copyright 2013, ClientEngage (http://www.clientengage.com)
 * @link            http://www.clientengage.com ClientEngage
 * @since           ClientEngage - Visitor Voice v 1.0
 * 
 */
App::uses('AppController', 'Controller');

/**
 * Fields Controller
 *
 * @property Field $Field
 */
class FieldsController extends AppController
{

    public $name = 'Fields';

    public function admin_addfield($visitorform_id = null, $type = null)
    {
        $this->set('title_for_layout', __('Add Field'));

        $this->layout = 'modal';

        if ($this->request->is('post'))
        {
            $this->Field->create();
            if ($this->Field->save($this->request->data))
            {
                $this->Session->setFlash(__('The field has been saved'), Flash::Success);
                $this->redirect(array('action' => 'editfield', $this->Field->id));
            }
            else
            {
                $this->Session->setFlash(__('The field could not be saved. Please, try again.'), Flash::Warning);
            }
        }
        else
        {
            $this->request->data['Field']['visitorform_id'] = $visitorform_id;
            $this->request->data['Field']['type'] = $type;
        }



        $this->set('validationRules', ClassRegistry::init('ValidationRule')->getSelectionList());
        $this->set('field', array('Field' => array('type' => $type)));
    }

    public function admin_editfield($id = null)
    {
        $this->set('title_for_layout', __('Edit Field'));

        $this->Field->id = $id;
        $this->layout = 'modal';

        if (!$this->Field->exists())
        {
            throw new NotFoundException(__('Invalid field'));
        }

        if ($this->request->is('post') || $this->request->is('put'))
        {
            if (isset($this->request->data['FieldOption']))
            {
                foreach ($this->request->data['FieldOption'] as $k => $opt)
                    if (!isset($opt['id']) && trim($opt['title']) == '')
                        unset($this->request->data['FieldOption'][$k]);

                $this->request->data['FieldOption'] = array_merge($this->request->data['FieldOption']);
            }

            if ($this->Field->saveAll($this->request->data))
            {
                $this->Session->setFlash(__('The field has been saved'), Flash::Success);
            }
            else
            {
                $this->Session->setFlash(__('The field could not be saved. Please, try again.'), Flash::Warning);
            }
        }
        else
        {
            $this->request->data = $this->Field->read(null, $id);
        }
        $this->set('validationRules', ClassRegistry::init('ValidationRule')->getSelectionList());
        $field = $this->Field->find('first', array('conditions' => array('Field.id' => $id), 'contain' => 'FieldOption'));
        $this->set('field', $field);
    }

    /**
     * delete method
     *
     * @throws MethodNotAllowedException
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null)
    {
        if (!$this->request->is('post'))
        {
            throw new MethodNotAllowedException();
        }
        $this->Field->id = $id;
        if (!$this->Field->exists())
        {
            throw new NotFoundException(__('Invalid field'));
        }

        $visitorform_id = $this->Field->field('visitorform_id');
        if ($this->Field->delete())
        {
            $this->Session->setFlash(__('Field deleted'), Flash::Success);
            $this->redirect(array('controller' => 'visitorforms', 'action' => 'view', $visitorform_id));
        }
        $this->Session->setFlash(__('Field was not deleted'), Flash::Error);
        $this->redirect(array('controller' => 'visitorforms', 'action' => 'view', $visitorform_id));
    }

    /**
     * Moves the passed field one position up
     * @param string $id Id of the phase
     */
    public function admin_moveup($id = null)
    {
        $this->Field->moveHigher($id);
        $this->redirect($this->referer());
    }

    /**
     * Moves the passed field one position down
     * @param string $id Id of the phase
     */
    public function admin_movedown($id = null)
    {
        $this->Field->moveLower($id);
        $this->redirect($this->referer());
    }

}
